package com.heima.behavior.controller;

import com.heima.behavior.dto.BehaviorDto;
import com.heima.behavior.entity.ApLikesBehavior;
import com.heima.behavior.service.IApLikesBehaviorService;
import com.heima.common.Threadlocals.UserThreadLocalUtil;
import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.enums.AppHttpCodeEnum;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * APP点赞行为表 前端控制器
 * </p>
 * @author itheima
 * @since 2022-08-04
 */
@RestController
@RequestMapping("/api/v1/likes_behavior")
@Api(tags = "APP点赞行为表接口")
@CrossOrigin
public class ApLikesBehaviorController{


    @Autowired
    private IApLikesBehaviorService apLikesBehaviorService;


    /**
     * 根据文章ID和设备ID查询是否点赞
     * @param articleId
     * @param entryId
     * @return
     */
    @GetMapping
    public ApLikesBehavior findApLikesBehavior(@RequestParam("articleId") Integer articleId,@RequestParam("entryId") Integer entryId){
        return apLikesBehaviorService.findByArticleIdAndEntryId(articleId,entryId);
    }

    /**
     * 保存点赞行为
     * @param dto
     * @return
     */
    @PostMapping
    public ResponseResult saveLikes(@RequestBody BehaviorDto dto) {
        User user = UserThreadLocalUtil.get();
        if(user==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        dto.setUserId(user.getUserId());
        return apLikesBehaviorService.like(dto);
    }
}
